package com.itheima.类加载与反射.反射.案例四;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class 反射对象 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //demo1();
        //demo2();
    }

    public static void demo2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("com.itheima.类加载与反射.反射.案例四.Student");
        Constructor con = aClass.getDeclaredConstructor(String.class);
        con.setAccessible(true);
        Student stu = (Student) con.newInstance("hello");
        System.out.println(stu);
    }

    public static void demo1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("com.itheima.类加载与反射.反射.案例四.Student");
        Constructor con = aClass.getConstructor();
        Student stu = (Student) con.newInstance();
        System.out.println(stu);
//        Class aClass = Class.forName("com.itheima.类加载与反射.反射.案例四.Student");
        Student stu1 = (Student) aClass.newInstance();
        System.out.println(stu1);
    }
}
